<?php

class DefaultController extends Controller
{
    public function actionMail()
    {
        $to = "duanhdan@gmail.com";
        $subject = "Test mail";
        $message = "Hello! This is a simple email message.";
        $from = "someonelse@example.com";
        $headers = "From:" . $from;
        mail($to,$subject,$message,$headers);
        echo "Mail Sent.";
    }
	public function actionIndex()
	{
		$this->layout = '//layouts/homeDefaultIndex';
		// Get top 10 nearest events
		$criteria = new CDbCriteria();
		$criteria->condition = 'status="A" AND DATE(startdate) >= NOW()';
		$criteria->limit = 10;
		$criteria->order = 'updatedate DESC,startdate ASC,enddate ASC';
		
		$model = Events::model()->findAll($criteria);
		
		// Get top 5 slider events
		$sliders = Events::model()->findAll('featured="Y" ORDER BY posteddate DESC LIMIT 5');
		// Get top 4 top events
		$topevents = Events::model()->findAll('hot="Y" ORDER BY posteddate DESC LIMIT 4');
        // Get top 5 homepage events
        $homepage = Events::model()->findAll('homepage="Y" ORDER BY posteddate DESC LIMIT 5');
		// Get top 5 news
		$news = News::model()->findAll('featured="Y" ORDER BY posteddate DESC LIMIT 5');
        // Get top 20 logs
        $logs = Logs::model()->findAll('id>0 ORDER BY id DESC LIMIT 20');
		// Get list categories
		$categories = Categories::model()->findAll('status="A" AND parent_id=0 ORDER BY position DESC');
		foreach ($categories as $category) {
			if (!empty($category->categories)) {
				foreach ($category->categories as $childCategory) {
					$items[] = array(
						'label'=>$childCategory->title,
						'url'=>array('/Events/categories/view', 'url'=>$childCategory->url),
						'linkOptions'=>array('title'=>'Xem sự kiện trong ' . $childCategory->title),
					);
				}
				$categoriesArr[] = array(
					'label'=>$category->title,
					'url'=>array('/Events/categories/view', 'url'=>$category->url),
					'linkOptions'=>array('title'=>'Xem sự kiện trong ' . $category->title),
					'items'=>$items,
				);
				unset($items);
			}
			else {
				$categoriesArr[] = array(
					'label'=>$category->title,
					'url'=>array('/Events/categories/view', 'url'=>$category->url),
					'linkOptions'=>array('title'=>'Xem sự kiện trong ' . $category->title),
				);
			}
		}
		// Render to the view
		$this->render('index', array(
			'model'=>$model,
			'sliders'=>$sliders,
			'topevents'=>$topevents,
			'categories'=>$categoriesArr,
			'news'=>$news,
            'logs'=>$logs,
            'homepage'=>$homepage
		));
	}

	function actionViewArticles($slug){
        $model = Articles::model()->findByAttributes(array('slug'=>$slug));
        
        if (empty($model)) {
            throw new CHttpException(404,'The requested page does not exist.');
        }
        
        $this->render('viewArticles',array(
            'model'=>$model
        ));
	}

	public function actionContact()
	{
		$model=new ContactForm;
		if(isset($_POST['ContactForm']))
			{
				$model->attributes=$_POST['ContactForm'];
				if($model->validate())
				{
					$name='=?UTF-8?B?'.base64_encode($model->name).'?=';
					$subject='=?UTF-8?B?'.base64_encode($model->subject).'?=';
					$headers="From: $name <{$model->email}>\r\n".
						"Reply-To: {$model->email}\r\n".
						"MIME-Version: 1.0\r\n".
						"Content-Type: text/plain; charset=UTF-8";
					$adminEmail = '' ;
					switch ($_POST['ContactForm']['toEmail']) {
						case '1':
							$adminEmail = Yii::app()->params['sales'] ;
							break;
						case '2':
							$adminEmail = Yii::app()->params['marketing'];
							break;
						case '3':
							$adminEmail = Yii::app()->params['tech'];
							break;
						case '4':
							$adminEmail = Yii::app()->params['adminEmail'];
							break;
					}
					mail($adminEmail,$subject,$model->body,$headers);
					Yii::app()->user->setFlash('contact','Thank you for contacting us. We will respond to you as soon as possible.');
					$this->refresh();
				}
			}
	
		$this->render('contact',array('model'=>$model));
	}
}